// pages/goods/show.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: [],
    goodslide: [],
    selected: true,
    selected1: false,
    goods_num: "",
    good: [],
    goodintro: [],
    goodparameter: [],
    goodversion: [],
    goodattr: {},
    gooddetail: [],
    slide: [],
    Authorization: '',
  },
  selectBrief(e) {
    this.setData({
      selected: true,
      selected1: false
    });
  },
  selectParameter(e) {
    this.setData({
      selected: false,
      selected1: true
    });
  },
  //点击加入购物车按钮跳到商品属性选择页面
  toSelect(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../selectGoods/selectGoods?id=${id}`
    });
  },
  goCart() {
    wx.switchTab({
      url: "../cart/cart"
    })
  },
  previewImage(e) {
    const index = e.currentTarget.dataset.index; //获取swiper里的图片的下标
    const slide = this.data.goodslide; //获取商品轮播图
    const imgList = []; //定义一个数组来存放轮播图的url
    // console.log(slide);
    slide.map(item => {
      imgList.push(item.image);
    });
    wx.previewImage({
      current: imgList[index], // 当前显示图片的链接，不填则默认为 urls 的第一张
      urls: imgList
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '请稍等',
    });
    var that = this;
    var Authorization = wx.getStorageSync('Authorization');
    console.info("wx Authorization is" + Authorization);
    const id = options.id;
    if (Authorization == '') {
      wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
      wx.reLaunch({
        url: '/pages/openid/openid',
      })
    } else {
      that.setData({
        Authorization: Authorization,
      })
    }
    console.info("show Authorization is:" + that.data.Authorization);
    //console.log(options);
    console.log('id is' + id);
    wx.request({
      url: "http://localhost:8080/wechat/selectgooddetail",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': that.data.Authorization
      },
      data: {
        'id': id,
      },
      success: function (res) {
        console.log(res);
        if (res.statusCode == 200) {
          that.setData({
            good: res.data["goods"],
            gooddetail: res.data,
            goodslide: res.data["goodSlideList"],
            goodintro: res.data["goodIntroList"],
            goodparameter: res.data["goodParameterList"],
            goodversion: res.data['goodVersionList'],
            goodcolor: res.data['goodcolor'],
          })
          wx.setStorageSync('gooddetail', that.data.gooddetail)
          console.info(res);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.hideLoading();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  //把用户选好的商品规格从缓存拿出来
  onShow: function () {
    const goods_num = wx.getStorageSync('goods_sum');
    this.setData({
      goods_num
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})